<?php
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class DocarioModelListport extends JModel {
    var $_catid = 0;
    var $_user = null;
    var $_groups = array();
    
    function __construct() {
        parent::__construct();
        $this->_user = JFactory::getUser();
        $this->_initUserGroups();
    }
    
    function _initUserGroups() {
        $query = 'select group_id from jos_docario_group_member';
        $query .= ' where userid='.$this->_user->id;
        
        $this->_db->setQuery($query);
        $this->_groups = $this->_db->loadResultArray();
    }
    
    function setCatId($catid) { $this->_catid = $catid; }
    
    function getCategories() {
        $query = 'select a.* from #__docario_category a';
        $query .= ' left join #__docario_category_user_allowed b on b.category_id=a.id';
        $query .= ' left join #__docario_category_group_allowed c on c.category_id=a.id';
        $query .= ' where a.parent_id='.$this->_catid;
        $query .= ' and ((b.userid='.$this->_user->id.' and b.read=1)';
        $query .= ' or ((b.userid is null or b.read=0) and (0=1';
        foreach ($this->_groups as $group) {
            $query .= ' or (';
            $query .= ' c.group_id='.$group;
            $query .= ' and c.read=1)';
        }
        $query .= '))) group by a.id';
        
        $this->_db->setQuery($query);
        return $this->_db->loadObjectList();
    }
    
    function getFiles() {
        $query = 'select a.* from #__docario_file a';
        $query .= ' left join #__docario_file_user_allowed b on b.file_id=a.id';
        $query .= ' where (b.userid is null or';
        $query .= ' (b.userid='.$this->_user->id.' and b.read=1)) and catid='.$this->_catid;
        
        $this->_db->setQuery($query);
        echo $this->_db->getQuery();
        return $this->_db->loadObjectList();
    }
    
    function getCategory() {
        $query = 'select * from #__docario_category';
        $query .= ' where id='.$this->_catid;
        
        $this->_db->setQuery($query);
        return $this->_db->loadObject();
    }
    
    function getBreadCrumb(&$cat_ids = array()) {
        if (count($cat_ids) > 0) {
            $last_element = $cat_ids[count($cat_ids)-1];
        } else $last_element = $this->_catid;
        
        $query = 'select parent_id from #__docario_category';
        $query .= ' where id='.$last_element;
        
        $this->_db->setQuery($query);
        $parent_id = $this->_db->loadResult();
        
        if ($parent_id > 0) {
            $cat_ids[] = $parent_id;
            $this->getBreadCrumb($cat_ids); // feel the dark side of resursion in PHP
        } else {
            $cat_objs = array();
            
            foreach ($cat_ids as $cat_id) {
                $query = 'select * from #__docario_category where id='.$cat_id;
                $this->_db->setQuery($query);
                $cat_objs[] = $this->_db->loadObject();
            }
            
            $cat_ids = $cat_objs;
        }
        
        return $cat_ids;
    }
}
